package com.android.dongshouzhi.fragment.member;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.dongshouzhi.R;
import com.android.dongshouzhi.activity.BaseObject;
import com.android.dongshouzhi.activity.mine.TransparentActivity;
import com.android.dongshouzhi.fragment.BaseLazyFragment;
import com.android.dongshouzhi.model.member.InvitationData;
import com.android.dongshouzhi.model.member.InvitationDataCallback;
import com.android.dongshouzhi.utils.ConstantsUrl;
import com.android.dongshouzhi.utils.HttpUtils;
import com.android.dongshouzhi.utils.ObjectUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 邀请好友
 */
public class Fragment_Mine_Invitation extends BaseLazyFragment {

    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.invitation_content)
    TextView content;

    private FragmentActivity mcontext;
    private View view;
    private boolean networkConnected;

    public Fragment_Mine_Invitation() {

    }

    public static Fragment_Mine_Invitation newInstance() {
        Fragment_Mine_Invitation fragment = new Fragment_Mine_Invitation();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initPrepare() {
    }

    @Override
    protected void onInvisible() {
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBER_INVITATION;
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            HttpUtils.get(address, header, null, dataCallback);
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    private String shareTitle;
    private String shareContent;
    private String shareUrl;
    private String sharePic;
    InvitationDataCallback dataCallback = new InvitationDataCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
        }

        @Override
        public void onResponse(InvitationData response, int id) {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
            if (response.getCode() == 0) {
                InvitationData.DataBean data = response.getData();
                String explain = data.getExplain();
                InvitationData.DataBean.CommentBean comment = data.getComment();
                shareTitle = comment.getSite_name();
                shareContent = comment.getText();
                shareUrl = comment.getUrl();
                sharePic = comment.getPic();
                content.setText(Html.fromHtml(explain));
            } else {
                noData.setVisibility(View.VISIBLE);
                noDataTv.setText(response.getMsg());
                noDataTv.setVisibility(View.VISIBLE);
            }
        }
    };

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine_invitation, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    @OnClick({R.id.click_resetnetwork_refresh, R.id.tv_my_invitation, R.id.tv_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.click_resetnetwork_refresh:
                getData();
                break;
            case R.id.tv_my_invitation:
                Intent intent = new Intent(mcontext, BaseObject.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "我的邀请");
                mcontext.startActivity(intent);
                break;
            case R.id.tv_share:
                ToShare();
                break;
        }
    }

    private void ToShare() {
        if(shareUrl==null){
            ObjectUtils.toast(mcontext, "正在加载，请稍后再试！");
            return;
        }
        Intent share = new Intent(mcontext, TransparentActivity.class);
        share.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_TRANS_SHOW_TYPE, 1);
        share.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_TRANS_SHOW_TITLE, shareTitle + "  " +shareContent);
        share.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_TRANS_SHOW_CONTENT, shareContent);
        share.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_TRANS_SHOW_IMAGEURL, sharePic);
        share.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_TRANS_SHOW_LINK, shareUrl);
        mcontext.startActivity(share);
        ((Activity) mcontext).overridePendingTransition(R.anim.es_snack_in, 0);
    }
}
